package me.firstapp.repository.shop.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.shop.DistributeCall;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.shop.DistributeCallRepository;

@Repository
public class DistributeCallRepositoryImpl extends EntityRepositoryImpl<DistributeCall>
		implements DistributeCallRepository {

	@Override
	public Page<DistributeCall> findDistributeCallPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM DistributeCall bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		if (searchParams != null) {
			Set<String> keys = searchParams.keySet();
			for (String key : keys) {
				Object value = searchParams.get(key);
				if (value != null) {
					if ("status".equals(key)) {
						String _value = (String) value;
						Integer status = Integer.valueOf(_value);
						sql.append(" AND bean.status =:status ");
						parameters.put("status", status);
					} else {
						sql.append(" AND bean." + key + " LIKE :value ");
						parameters.put("value", "%" + value + "%");
					}
				}
			}
		}
		
		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		if (StrUtils.isNotNULL(sortParam) && StrUtils.isNotNULL(sortType)) {
			sql.append(" ORDER BY " + sortParam + " " + sortType);
		}

		return super.queryForPage(DistributeCall.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	protected Class<DistributeCall> getPersistentClass() {
		return DistributeCall.class;
	}

	@Override
	public DistributeCall findByMemberIdAndOrderNo(Long memberId, String orderNo, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM DistributeCall bean WHERE 1=1 ");

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		
		sql.append(" AND bean.memberId =:memberId ");
		parameters.put("memberId", memberId);

		sql.append(" AND bean.orderNo =:orderNo ");
		parameters.put("orderNo", orderNo);

		List<DistributeCall> result = super.query(DistributeCall.class, sql.toString(), parameters);

		return result.isEmpty() ? null : result.get(0);
	}

	@Override
	public DistributeCall findByOrderNo(String orderNo, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM DistributeCall bean WHERE 1=1 ");

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		
		sql.append(" AND bean.orderNo =:orderNo ");
		parameters.put("orderNo", orderNo);

		List<DistributeCall> result = super.query(DistributeCall.class, sql.toString(), parameters);

		return result.isEmpty() ? null : result.get(0);
	}

}
